#################

# Causes of perceived government trustworthiness
# European Journal of Political Research
# Replication code -- please see the readme.
# UK CONJOINT ONLY

#################

remove(list=ls())

install.packages("pacman")
library(pacman)

p_load(here, rio, ggpubr, dplyr, jtools, estimatr, grf, MASS, stargazer,
       reporttools, nnet, margins, panelr, cregg, lmtest, FindIt, viridis)

uk_conjointdf <- import(here("data", "uk_conjointdf.RData"))

# --- Figure 2 ----

uk_mm <- cj(uk_conjointdf, trust_more ~ Performance__1 + Performance__2 + Interests_1 + Interests_2 + 
              Levels_of_corruption + Transparency + Time_in_office + Political_outlook +
              Top_governing_priority + Approval_rating_in_recent_polls , 
            estimate = "mm", h0 = 0.5, id = ~id, 
            feature_labels = list("Performance__1" = "Comp. I", "Performance__2" = "Comp. II",
                                  "Interests_1" = "Benev. I", "Interests_2" = "Benev. II",
                                  "Levels_of_corruption" = "Integrity I", "Time_in_office" = "Tenure",
                                  "Political_outlook" = "Ideology", "Top_governing_priority" = "Priorities",
                                  "Approval_rating_in_recent_polls" = "Approval",
                                  "Transparency" = "Integrity II"))


uk_mm %>% 
  ggplot(aes(level)) +
  geom_hline(yintercept = 0.5, color="grey10") +
  geom_linerange(aes(y=estimate, ymin= lower, ymax = upper, color = feature),
                 linewidth=3, position = position_dodge(width = 0.9), alpha = 0.7) +
  # geom_linerange(aes(y=estimate, ymin= lower, ymax = upper, colour = feature),
  #                linewidth=0.7, position = position_dodge(width = 0.9), alpha = 0.5) +
  geom_point(aes(y = estimate, color = feature), position = position_dodge(width=0.1), size = 4) + 
  facet_grid(feature~., scales='free', space='free') +
  coord_flip() +
  labs(x=NULL, y='Marginal means') +
  theme_classic() +
  theme(
    strip.placement = 'outside',
    panel.spacing = unit(2,'pt'),
    panel.background = element_rect(color='black'),
    legend.position = "none",
    legend.title = element_blank(),
    strip.background= element_blank(),
    strip.text = element_text(size = 8.5),
    axis.title.x = element_text(size = 14),
    axis.text.x = element_text(size = 16),
    axis.text.y = element_text(face="bold", size = 14)) +
  guides(fill = guide_legend(reverse=T)) +
  scale_fill_viridis(discrete = TRUE, option = "plasma", 
                     begin = 0.15,
                     end = 0.8, direction = 1) +
  scale_colour_viridis(discrete = TRUE, option = "plasma", 
                       begin = 0.15,
                       end = 0.8, direction = 1) +
  geom_text(
    aes(label = sprintf("%0.2f", round(estimate, digits = 2)), x = level, y = 0.3, color = feature),
    position = position_dodge(width = 0.9), size = 4, fontface = "bold",
  )

ggsave(here("figures", "uk_mm.png"),
       width = 30, height = 24, units = "cm",
       dpi = 600)

